<template>
  <div class="food-content-box restaurant-list-box">
    <el-breadcrumb separator="/" class="page-title">
      <el-breadcrumb-item>Bus Line</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="container-padding">
      <el-form :inline="true" label-position="top" :model="searchForm" size="mini" ref="searchForm"  class="form-min-width">
        <div class="container-box label-style">
          <el-row :gutter="20">
            <el-col>
              <el-form-item :label="$t('m.Status')">
                <el-select  style="width: 200px" v-model="searchForm.status" placeholder="" clearable>
                  <el-option v-for="rg in status" :key="rg.value" :label="rg.text" :value="rg.value"></el-option>
                </el-select>
              </el-form-item>
              <span style="display:inline-block;margin-top:36px;">
                <el-button type="warning" @click="getList" size="small">{{$t('m.Search')}}</el-button>
                <el-button @click="$router.push('/busLineNew')" size="small">{{$t('m.New')}}</el-button>
              </span>
              <el-form-item style="margin: 36px 0 0 10px;">
                <el-upload
                  class="upload-demo"
                  :action="uploadUrl"
                  name="file"
                  :on-preview="handlePreview"
                  :auto-upload="true"
                  :on-remove="handleRemove"
                  :before-remove="beforeRemove"
                  :on-progress="handleProgress"
                  :on-success="onUploadSuccess"
                  :on-error="onUploadError"
                  multiple
                  :limit="1"
                  :show-file-list="false"
                  :on-exceed="handleExceed"
                  :file-list="fileList">
                  <el-button size="small" style="width: 200px;">Upload excel (bus line order)</el-button>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <!--表格-->
      <el-table
        :data="tableData"
        v-loading="tableLoading"
        border
        style="width: 100%;margin-top:20px;">
        <!--Edit Bus Line-->
        <el-table-column
          prop="Edit"
          label="Edit Bus Line"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <!--<el-button size="mini" width="40"  @click="editRestaurant(scope.id)">{{$t('m.Edit')}}</el-button>-->
            <el-button type="text" size="small" @click="editBusLine(scope.row.id)">{{$t('m.Edit')}}</el-button>
          </template>
        </el-table-column>
        <!--Status-->
        <el-table-column
          prop="Status"
          label="Status"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <div class="list-notes">{{getStatusItem(scope.row.status)}}</div>
          </template>
        </el-table-column>
        <!--Sorting order-->
        <el-table-column
          prop="Sorting order"
          label="Sorting order"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <div class="list-notes">{{scope.row.sequence}}</div>
          </template>
        </el-table-column>
        <!--Line No-->
        <el-table-column
          prop="Line No"
          label="Line No"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <div class="list-notes">{{scope.row.lineNum}}</div>
          </template>
        </el-table-column>
        <!--Type-->
        <el-table-column
          prop="Type"
          label="Type"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <div class="list-notes">{{scope.row.type}}</div>
          </template>
        </el-table-column>
        <!--Name-->
        <el-table-column
          prop="Name"
          label="Name"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <div class="list-notes">{{scope.row.name}}</div>
          </template>
        </el-table-column>
        <!--Base-->
        <el-table-column
          prop="Base"
          label="Base"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <div class="list-notes">{{scope.row.start}}</div>
          </template>
        </el-table-column>
        <!--Destination-->
        <el-table-column
          prop="Destination"
          label="Destination"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <div class="list-notes">{{scope.row.end}}</div>
          </template>
        </el-table-column>
        <!--Color-->
        <el-table-column
          prop="Color"
          label="Color"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <div class="list-notes">{{scope.row.color}}</div>
          </template>
        </el-table-column>
        <!--Created Date-->
        <el-table-column
          prop="CreatedDate"
          label="Created Date"
          align="center"
          min-width="160">
          <template slot-scope="scope">
            <div class="list-notes">{{getDate(scope.row.createDate)}}</div>
          </template>
        </el-table-column>
        <!--Created By-->
        <el-table-column
          prop="CreatedBy"
          label="Created By"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <div class="list-notes">{{scope.row.createBy}}</div>
          </template>
        </el-table-column>
        <!--Updated Date-->
        <el-table-column
          prop="UpdatedDate"
          label="Updated Date"
          align="center"
          min-width="160">
          <template slot-scope="scope">
            <div class="list-notes">{{getDate(scope.row.updateDate)}}</div>
          </template>
        </el-table-column>
        <!--Updated By-->
        <el-table-column
          prop="UpdatedBy"
          label="Updated By"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <div class="list-notes">{{scope.row.updateBy}}</div>
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <div class="block" style="margin-top: 15px;text-align: center;">
        <el-pagination
          @size-change="handleChangeSize"
          @current-change="handleChangePage"
          :current-page="pageData.pageNo"
          :page-size="pageData.pageSize"
          :page-sizes="[10, 20, 50, 100, 500]"
          :page-count="pageInfo.totalPages"
          :total="pageInfo.totalCount"
          layout="total, prev, pager, next, jumper, sizes">
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
  import { searchBusLineList } from '../../api/mgc_api'
  import {uploadUrlCommon} from '../../api/zxy_api'
  export default {
    name: 'BusLineIndex',
    data () {
      return {
        uploadUrl: uploadUrlCommon + '/admin/busLine/excel-upload',
        fileList: [],
        loading:null,
        status: [
          {"value": 1, "text": "Active"},
          // {"value": 2, "text": "Suspended"},
          {"value": 0, "text": "Deleted"},
        ],
        searchForm: {
          // 表单数据必填字段
          status: 1,
        },
        tableData: [],
        pageData: {
          pageNum: 1,
          pageSize: 20,
        },
        pageInfo: {
          totalCount: 0,
          totalPages: 0
        },
        tableLoading: false,
      }
    },
    methods: {
      handleChangeSize (val) {
        this.pageData.pageSize = parseInt(val)
        this.pageData.pageNum = 1
        this.getList()
      },
      handleChangePage (val) {
        this.pageData.pageNum = val
        this.getList()
      },
      getStatusItem (it) {
        let str = ''
        this.status.forEach((item) => {
          if (item.value == it) {
            str = item.text || ''
          }
        })
        return str
      },
      getDate (value) {
        if(!value){
          return ''
        }
        var date = new Date(value)
        var Y = date.getFullYear()
        var m = date.getMonth() + 1
        var d = date.getDate()
        var H = date.getHours()
        var i = date.getMinutes()
        var s = date.getSeconds()
        if (m < 10) {
          m = '0' + m
        }
        if (d < 10) {
          d = '0' + d
        }
        if (H < 10) {
          H = '0' + H
        }
        if (i < 10) {
          i = '0' + i
        }
        if (s < 10) {
          s = '0' + s
        }
        var ts = Y + '-' + m + '-' + d + '  ' + H + ':' + i + ':' + s
        return ts
      },
      editBusLine (id) {
        this.$router.push({
          path: '/busLineNew',
          query: {
            id: id
          }
        })
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        // this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      // 文件上传时候的钩子
      handleProgress() {
        this.loading = this.$loading({
          lock: true,
          text: 'Uploading',
          spinner: 'el-icon-loading',
          background: 'rgba(255, 255, 255, 0.7)'
        });
      },
      // 文件上传成功的钩子
      onUploadSuccess(response, file, fileList) {
        this.loading.close();
        console.log(response, 'success')
        if (response.errorID == 0) {
          this.fileList = [];
          this.$message({
            showClose: true,
            message: 'success',
            duration: 1500,
            type: 'success'
          })
          this.getList()
        }else {
          this.fileList = [];
          this.$message({
            showClose: true,
            message: response.message,
            duration: 1500,
            type: 'error'
          })
        }
      },
      // 文件上传失败的钩子
      onUploadError(err, file, fileList) {
        this.fileList = [];
        console.log(err, 'err')
      },
      getList () {
        this.tableLoading = true
        searchBusLineList(Object.assign(this.searchForm, this.pageData))
          .then((res) => {

            // this.showToast(res.data.errorID)
            // if (res.data.errorID === 100004) {
            //   this.$router.push('/login')
            //   return
            // }
            let data = res
            this.tableData = data.data.list
            // 分页信息配置
            this.pageInfo.totalPages = data.data.pages
            this.pageInfo.totalCount = data.data.total
            // 清除loading 状态
            setTimeout(() => {
              this.tableLoading = false
            }, 400)
          })
          .catch((error) => {
            // this.showToast(error.errorID)
            setTimeout(() => {
              this.tableLoading = false
            }, 400)
          })
      },
    },
    mounted () {
      this.getList()
    }
  }
</script>
<style scoped>
  .page-title {
    padding: 20px 40px;
    border-bottom: 1px solid #e6e6e6;
    font-size: 20px;
  }
  .food-content-box .container-padding {
    padding: 20px 40px 0 40px;
  }
  .list-notes{
    text-align: center;
    display: block;
  }
</style>
